package com.waitingresult.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: WangShuai
 * @CreateTime: 2022-11-30  11:40
 * @Description: 物流轨迹
 * @Version: 1.0
 */
@Slf4j
@Configuration
@EnableRabbit
public class CalaculateRabbitMqConfig {

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

    /**
     * =========================   发货仓死信队列  =========================
     */
    @Bean
    public Queue scpCalculateContextQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "SCP_OFC_CALCULATE_STORE_DEAD_EXCHANGE");
        args.put("x-dead-letter-routing-key", "SCP_OFC_CALCULATE_STORE_DEAD_QUEUE_KEY");
        return new Queue("SCP_OFC_CALCULATE_STORE_QUEUE", true, false, false, args);
    }

    @Bean
    public TopicExchange bulidCalculateContextQueueExchange() {
        return new TopicExchange("SCP_OFC_CALCULATE_STORE_EXCHANGE");
    }

    @Bean
    public Binding bindScpCalculateContextQueueDirect() {
        return BindingBuilder
                .bind(scpCalculateContextQueue())
                .to(bulidCalculateContextQueueExchange())
                .with("SCP_OFC_CALCULATE_STORE_QUEUE_KEY");
    }

    @Bean
    public Queue deadLetterQueue() {
        return new Queue("SCP_OFC_CALCULATE_STORE_DEAD_QUEUE");
    }


    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("SCP_OFC_CALCULATE_STORE_DEAD_EXCHANGE", true, false);
    }

    /**
     * 渠道回写死信队列
     */
    @Bean
    public Binding deadLetterBindding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("SCP_OFC_CALCULATE_STORE_DEAD_QUEUE_KEY");
    }


    /**
     * =========================   校验队列  =========================
     */
    @Bean
    public Queue scpCalculateValidationQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "SCP_OFC_CALCULATE_VALIDATION_DEAD_EXCHANGE");
        args.put("x-dead-letter-routing-key", "SCP_OFC_CALCULATE_VALIDATION_DEAD_QUEUE_KEY");
        return new Queue("SCP_OFC_CALCULATE_VALIDATION_QUEUE", true, false, false, args);
    }

    @Bean
    public DirectExchange bulidCalculateValidationQueueExchange() {
        return new DirectExchange("SCP_OFC_CALCULATE_VALIDATION_EXCHANGE");
    }

    @Bean
    public Binding bindScpCalculateValidationQueueDirect(Exchange bulidCalculateValidationQueueExchange, Queue scpCalculateValidationQueue) {
        return BindingBuilder
                .bind(scpCalculateValidationQueue)
                .to(bulidCalculateValidationQueueExchange)
                .with("SCP_OFC_CALCULATE_VALIDATION_QUEUE_KEY").noargs();
    }


}
